package com.toman.Adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.toman.Entity.MyBeltline_Entity;
import com.toman.MyApplication;
import com.toman.R;

import java.util.ArrayList;

/**
 * Created by  RemoteStart_adpter on Administrator on 2017/11/27 on 10:39.
 */

public class RemoteStart_adpter extends BaseAdapter {

    //上下文对象
    Context context;
    //
    ArrayList<MyBeltline_Entity> arrayList;

    LayoutInflater inflater;
    MyApplication myApplication;

    public RemoteStart_adpter(Context context, ArrayList<MyBeltline_Entity> arrayList, MyApplication myApplication) {
        this.context = context;
        this.arrayList = arrayList;
        this.myApplication = myApplication;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AddPackage addPackage;

        if (null == convertView) {
            addPackage = new AddPackage();
            convertView = inflater.inflate(R.layout.remotestart_item, null, false);
            addPackage.img = (ImageView) convertView.findViewById(R.id.mReImg);
            addPackage.name = (TextView) convertView.findViewById(R.id.mRename);
            addPackage.shut = (TextView) convertView.findViewById(R.id.mReshut);

            convertView.setTag(addPackage);
        } else {
            addPackage = (AddPackage) convertView.getTag();
        }
        //  addPackage.img.setBackgroundResource(arrayList.get(position).getImg());

        addPackage.name.setText(arrayList.get(position).getBeltline_name());
        if (arrayList.get(position).getOnoff().equals("ON")) {
            addPackage.shut.setText("已开启");
        } else if (arrayList.get(position).getOnoff().equals("YR")) {
            addPackage.shut.setText("预热中");
        } else {
            addPackage.shut.setText("已关闭");
        }

        if (arrayList.get(position).getOnoff().equals("ON")) {
            if (myApplication.getSp().getString("token", "").equals("toman")) {
                addPackage.img.setBackgroundResource(R.mipmap.jic2);
            } else if (myApplication.getSp().getString("token", "").equals("operater")) {
                addPackage.img.setBackgroundResource(R.mipmap.jic);

            } else if (myApplication.getSp().getString("token", "").equals("company")) {
                addPackage.img.setBackgroundResource(R.mipmap.jic3);
            }
        } else if (arrayList.get(position).getOnoff().equals("YR")) {
            addPackage.img.setBackgroundResource(R.mipmap.jic3);
        } else {
            addPackage.img.setBackgroundResource(R.mipmap.jic1);
        }

        if (arrayList.get(position).getOnoff().equals("ON")) {
            if (myApplication.getSp().getString("token", "").equals("toman")) {
                addPackage.shut.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.remote2));

            } else if (myApplication.getSp().getString("token", "").equals("operater")) {
                addPackage.shut.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.remote1));

            } else if (myApplication.getSp().getString("token", "").equals("company")) {
                addPackage.shut.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.remote3));

            }
        } else if (arrayList.get(position).getOnoff().equals("YR")) {
            addPackage.shut.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.remote5));

        } else {
            addPackage.shut.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.remote));

        }

        return convertView;
    }

    public class AddPackage {
        ImageView img;
        TextView name, shut;
    }


}
